##########################################################
# John Henderson and Alex Theodoridis
# Replication Data for: "Seeing Spots", 
#  Forthcoming in Political Behavior, August 20, 2017
# 
##########################################################
#
#  figure_2.R 
#  -- file produces the mean bar plots Figure 2
#
##########################################################

rm(list=ls())
source('~/Dropbox/Seeing_Spots/replication/pre_data.R')

ymat=cbind(video_skipped, replay, share, getlink, all_y,time_watched, total_time)   

# outcome matrices 
outcome1=outcome2=matrix(NA,7,4)
colnames(outcome1) = colnames(outcome2) = c('Mean Positive','Mean Negative','Difference','pvalue')  
rownames(outcome1) = rownames(outcome2) = c('Skipped','Replay','Share','Get Link','All','Time Watched','Total Time')

# results are based on bootstrap sampling: redundant reproduced below

# (a) pos v. neg
      
outcome1[,1]=colMeans(ymat[pos_ad,],na.rm=T)
outcome1[,2]=colMeans(ymat[!pos_ad,],na.rm=T)   
outcome1[,3]=outcome1[,1]-outcome1[,2]
pvals=array(1,7)
for(i in 1:7){pvals[i]=t.test(ymat[pos_ad,i],ymat[!pos_ad,i])$p.value}
outcome1[,4]=pvals

# (b) obama v. romney 
      
outcome2[,1]=colMeans(ymat[obama_ad,],na.rm=T)
outcome2[,2]=colMeans(ymat[!obama_ad,],na.rm=T)   
outcome2[,3]=outcome2[,1]-outcome2[,2]
pvals=array(1,7)
for(i in 1:7){pvals[i]=t.test(ymat[obama_ad,i],ymat[!obama_ad,i])$p.value}
outcome2[,4]=pvals  

# (c) obama v. romney v. pos v. neg

outcome3=matrix(NA,7,4)
colnames(outcome3) = c('Mean Positive & Obama','Mean Negative & Obama','Mean Positive & Romney','Mean Negative & Romney')  
rownames(outcome3) = c('Skipped','Replay','Share','Get Link','All','Time Watched','Total Time')                   

outcome3[,1]=colMeans(ymat[obama_pos,],na.rm=T)
outcome3[,2]=colMeans(ymat[obama_neg,],na.rm=T)         
outcome3[,3]=colMeans(ymat[romney_pos,],na.rm=T)
outcome3[,4]=colMeans(ymat[romney_neg,],na.rm=T)
           


outcomes = list('pos v. neg'=outcome1, 'obama v. romney'=outcome2, '2-way'=outcome3)

# main plots here 

# bootstrap approach
# MAIN EFFECTS;         
 

set.seed(1005)   
po_no_pr_nr=ovr=pvn=list() 
for(k in 1:5){
	pvn[[k]]=ovr[[k]]=matrix(NA,2000,2)
	po_no_pr_nr[[k]]=matrix(NA,2000,4)
}

ym=as.matrix(cbind(all_y,video_skipped,replay,share,getlink))
#colnames(ymat)=c('all','skip','replay','share','getlink')

for(j in 1:2000){
	inds=sample(1:length(tr),size=length(tr),replace=T)
	omega=tr[inds]   
	ymat=ym[inds,]
	       	
	ind1 = which(str_sub(omega,1,3) == 'Pos' & str_sub(omega,10,12) == "Oba")
	ind2 = which(str_sub(omega,1,3) == 'Neg' & str_sub(omega,10,12) == "Oba")
	ind3 = which(str_sub(omega,1,3) == 'Pos' & str_sub(omega,10,12) == "Rom")
	ind4 = which(str_sub(omega,1,3) == 'Neg' & str_sub(omega,10,12) == "Rom")
	
	s1=sort(c(ind1,ind3)) # positive
	s2=sort(c(ind2,ind4)) # negative
	
	s3=sort(c(ind1,ind2)) # obama        
	s4=sort(c(ind3,ind4)) # romney 
	
	for(k in 1:5){
		pvn[[k]][j,1:2] = c(mean(ymat[s1,k],na.rm=T),mean(ymat[s2,k],na.rm=T))
		ovr[[k]][j,1:2] = c(mean(ymat[s3,k],na.rm=T),mean(ymat[s4,k],na.rm=T))  
		po_no_pr_nr[[k]][j,1:4] = c(mean(ymat[ind1,k],na.rm=T),mean(ymat[ind2,k],na.rm=T),
								 mean(ymat[ind3,k],na.rm=T),mean(ymat[ind4,k],na.rm=T))
	}  
}
    
colnames(ymat)=c('all','skip','replay','share','getlink')    
pvn[[1]]=pvn[[1]]/4
ovr[[1]]=ovr[[1]]/4
po_no_pr_nr[[1]]=po_no_pr_nr[[1]]/4
  
# pos v. neg for partisans v. independents

# counting
out=c(all_y/4,video_skipped,replay,share,getlink)
tr_out=rep(pos_ad,5)
x_out=c(rep(0,length(all_y)),rep(1,length(all_y)),rep(2,length(all_y)),rep(3,length(all_y)),rep(4,length(all_y)))
ns=t(c(table(interaction(!tr_out[which(!is.na(out))],x_out[which(!is.na(out))]))))
  

{    
pdf(file='~/Dropbox/Seeing_Spots/replication/figures/pos_neg_all.pdf')  
	# skipping the video    
	plot(x=c(1,1),y=c(1,1),ylim=c(0,.6),xlim=c(1,10),pch='-',col='white',axes=F,xlab='',ylab='Ad-Seeking Choices',type='b')
	abline(v=c(2.5,4.5,6.5,8.5),lty=2)
	box()
	mtext(text='All Items',side=3,at=1.5,cex=1)
	mtext(text='Skip Ad',side=3,at=3.5,cex=1)
	mtext(text='Replay',side=3,at=5.5,cex=1)    
	mtext(text='Share',side=3,at=7.5,cex=1)    
	mtext(text='Get Link',side=3,at=9.5,cex=1)    
	axis(2)
	axis(1,at=c(1,2,3,4,5,6,7,8,9,10,11,12),labels=F)
	text(labels= c(paste("\nPositive\n(n=",ns[1],")",sep='')),x=1.1,y=-.055,srt=0,xpd=T,cex=.75)
	text(labels= c(paste("\nNegative\n(n=",ns[2],")",sep='')),x=2.1,y=-.055,srt=0,xpd=T,cex=.75)  
	text(labels= c(paste("\nPositive\n(n=",ns[3],")",sep='')),x=3.1,y=-.055,srt=0,xpd=T,cex=.75)
	text(labels= c(paste("\nNegative\n(n=",ns[4],")",sep='')),x=4.1,y=-.055,srt=0,xpd=T,cex=.75)
	text(labels= c(paste("\nPositive\n(n=",ns[5],")",sep='')),x=5.1,y=-.055,srt=0,xpd=T,cex=.75)
	text(labels= c(paste("\nNegative\n(n=",ns[6],")",sep='')),x=6.1,y=-.055,srt=0,xpd=T,cex=.75)
	text(labels= c(paste("\nPositive\n(n=",ns[7],")",sep='')),x=7.1,y=-.055,srt=0,xpd=T,cex=.75)
	text(labels= c(paste("\nNegative\n(n=",ns[8],")",sep='')),x=8.1,y=-.055,srt=0,xpd=T,cex=.75)
	text(labels= c(paste("\nPositive\n(n=",ns[9],")",sep='')),x=9.1,y=-.055,srt=0,xpd=T,cex=.75)
	text(labels= c(paste("\nNegative\n(n=",ns[10],")",sep='')),x=10.1,y=-.055,srt=0,xpd=T,cex=.75)                                                                  
	eps=.0015
	cols='black'
	cnts=0        
	
	for(k in 1:5){
	j=1   
	cnts=cnts+1
	y1=quantile(pvn[[k]][,j],prob=c(.025,.975))
	y2=mean(pvn[[k]][,j])  
	j=cnts
	lines(x=c(j,j),y=c(y1[1]-eps,y1[2]+eps),col=cols,lwd=2.5,type='l')
	lines(x=c(j-.15,j+.15),y=c(y1[1]-eps,y1[1]-eps),lwd=2.5,col=cols,type='l')
	lines(x=c(j-.15,j+.15),y=c(y1[2]+eps,y1[2]+eps),lwd=2.5,col=cols)
	points(x=(j),y=y2,pch=15,cex=2.25,col='white')
	text(x=(j),y=y2,label=str_sub(as.character(y2),1,4),pch=19,cex=.75,col=cols)

	j=2 
	cnts=cnts+1  
	y1=quantile(pvn[[k]][,j],prob=c(.025,.975))
	y2=mean(pvn[[k]][,j])    
	j=cnts
	lines(x=c(j,j),y=c(y1[1]-eps,y1[2]+eps),col=cols,lwd=2.5,type='l')
	lines(x=c(j-.15,j+.15),y=c(y1[1]-eps,y1[1]-eps),lwd=2.5,col=cols,type='l')
	lines(x=c(j-.15,j+.15),y=c(y1[2]+eps,y1[2]+eps),lwd=2.5,col=cols)
	points(x=(j),y=y2,pch=15,cex=2.25,col='white')
	text(x=(j),y=y2,label=str_sub(as.character(y2),1,4),pch=19,cex=.75,col=cols) 
	}

dev.off()       
}       

# END figure_2.R  